package com.lbssoft.gpsbd.service.impl;

import com.alibaba.fastjson.JSON;
import com.lbssoft.datalink.util.HttpClientUtil;
import com.lbssoft.gpsbd.common.PropertiesUtil;
import com.lbssoft.gpsbd.common.Utils;
import com.lbssoft.gpsbd.model.vo.Developer;
import com.lbssoft.gpsbd.model.vo.SystemProperty;
import com.lbssoft.gpsbd.service.InstallService;
import org.springframework.boot.ApplicationHome;
import org.springframework.stereotype.Service;

import java.io.File;

/**
 * Created by nczc on 2018/9/4.
 */
@Service
public class InstallServiceImpl implements InstallService {
    @Override
    public boolean createDataBase(SystemProperty property) {
        StringBuilder createDataBase=new StringBuilder();
        createDataBase.append("mysqladmin").append(" -h").append(property.getDbHost());
        createDataBase.append(" -P").append(property.getDbPort());
        createDataBase.append(" -u").append(property.getDbUserName()).append(" -p").append(property.getDbPassword());
        createDataBase.append(" CREATE ").append(property.getDbName());
        return Utils.runCommand(createDataBase.toString());
    }

    @Override
    public boolean importData(SystemProperty property) {
        ApplicationHome home = new ApplicationHome(getClass());
        File jarFile = home.getSource();
        String sqlFileName = "gpsbd.sql";

        StringBuilder initData=new StringBuilder();
        initData.append("mysql").append(" -h").append(property.getDbHost());
        initData.append(" -P").append(property.getDbPort());
        initData.append(" -u").append(property.getDbUserName()).append(" -p").append(property.getDbPassword());
        initData.append(" ").append(property.getDbName());
        initData.append(" <").append(jarFile.getParentFile()).append(System.getProperty("file.separator") );
        initData.append(sqlFileName);
        initData.append(" ").append("--default-character-set=utf8");
        return Utils.runCommand(initData.toString());
    }


    @Override
    public boolean initProperties(SystemProperty property) {
        PropertiesUtil pro = new PropertiesUtil("application.properties");
        pro.put("spring.datasource.url", "jdbc:mysql://"+property.getDbHost()+":"+property.getDbPort()+"/"+property.getDbName()+"?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true");
        pro.put("spring.datasource.username", property.getDbUserName());
        pro.put("spring.datasource.password", property.getDbPassword());
        pro.put("spring.redis.host", property.getRedisHost());
        pro.put("spring.redis.port", property.getRedisPort());
        pro.put("spring.redis.password", property.getRedisPassword());
        pro.put("gpsbd.install", "true");
        pro.store("application.properties");

        return false;
    }

    @Override
    public String getAppSecret(Developer developer) {

        com.lbssoft.datalink.model.Result res =  HttpClientUtil.httpPost("http://baseapi.gpsbd.com:8086/users/register", JSON.toJSONString(developer));

        if(res.getCode()==0){
            PropertiesUtil pro = new PropertiesUtil("application.properties");
            pro.put("gpsbd.appsecret", res.getData().toString());
            pro.store("application.properties");
            return res.getData().toString();
        }
        return null;
    }
}
